/***************************************************************************
 *            interface.c
 *
 *  Sun Aug 13 13:03:57 2006
 *  Copyright  2006  Alexander P Telenga
 *  telenga@gmail.com
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "include/var.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget *
create_easyreader_main_window (void)
{
	GtkWidget *vbox1;
	GtkWidget *menubar1;
	GtkWidget *menuitem1;
	GtkWidget *menuitem1_menu;
	GtkWidget *open;
	GtkWidget *separatormenuitem1;
	GtkWidget *quit;
	GtkWidget *menuitem2;
	GtkWidget *menuitem2_menu;
	GtkWidget *copy;
	GtkWidget *clear;
	GtkWidget *font;
	GtkWidget *menuitem4;
	GtkWidget *menuitem4_menu;
	GtkWidget *About;
	GtkWidget *toolbar1;
	GtkIconSize tmp_toolbar_icon_size;
	GtkWidget *open_toolbutton;
	GtkWidget *separatortoolitem1;
	GtkWidget *copy_toolbutton;
	GtkWidget *clear_toolbutton;
	GtkWidget *font_toolbutton;
	GtkWidget *separatortoolitem2;
	GtkWidget *quit_toolbutton;
	GtkWidget *about_toolbutton;
	GtkWidget *scrolledwindow1;
	GtkAccelGroup *accel_group;
	GdkPixbuf *icon;

	accel_group = gtk_accel_group_new ();

	easyreader_main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (easyreader_main_window),
			      _("Easy reader"));
	icon = gdk_pixbuf_new_from_file ("easyreader.png", NULL);
	gtk_window_set_icon (GTK_WINDOW (easyreader_main_window), icon);
	gtk_window_set_position (GTK_WINDOW (easyreader_main_window),
				 GTK_WIN_POS_CENTER);
	gtk_window_set_default_size (GTK_WINDOW (easyreader_main_window), 800,
				     600);

	vbox1 = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox1);
	gtk_container_add (GTK_CONTAINER (easyreader_main_window), vbox1);

	menubar1 = gtk_menu_bar_new ();
	gtk_widget_show (menubar1);
	gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);

	menuitem1 = gtk_menu_item_new_with_mnemonic (_("Файл"));
	gtk_widget_show (menuitem1);
	gtk_container_add (GTK_CONTAINER (menubar1), menuitem1);

	menuitem1_menu = gtk_menu_new ();
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);

	open = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
	gtk_widget_show (open);
	gtk_container_add (GTK_CONTAINER (menuitem1_menu), open);

	separatormenuitem1 = gtk_separator_menu_item_new ();
	gtk_widget_show (separatormenuitem1);
	gtk_container_add (GTK_CONTAINER (menuitem1_menu),
			   separatormenuitem1);
	gtk_widget_set_sensitive (separatormenuitem1, FALSE);

	quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
	gtk_widget_show (quit);
	gtk_container_add (GTK_CONTAINER (menuitem1_menu), quit);

	menuitem2 = gtk_menu_item_new_with_mnemonic (_("Правка"));
	gtk_widget_show (menuitem2);
	gtk_container_add (GTK_CONTAINER (menubar1), menuitem2);

	menuitem2_menu = gtk_menu_new ();
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem2), menuitem2_menu);

	copy = gtk_image_menu_item_new_from_stock ("gtk-copy", accel_group);
	gtk_widget_show (copy);
	gtk_container_add (GTK_CONTAINER (menuitem2_menu), copy);

	clear = gtk_image_menu_item_new_from_stock ("gtk-clear", accel_group);
	gtk_widget_show (clear);
	gtk_container_add (GTK_CONTAINER (menuitem2_menu), clear);

	font = gtk_image_menu_item_new_from_stock ("gtk-select-font",
						   accel_group);
	gtk_widget_show (font);
	gtk_container_add (GTK_CONTAINER (menuitem2_menu), font);

	menuitem4 = gtk_menu_item_new_with_mnemonic (_("Справка"));
	gtk_widget_show (menuitem4);
	gtk_container_add (GTK_CONTAINER (menubar1), menuitem4);

	menuitem4_menu = gtk_menu_new ();
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);

	About = gtk_menu_item_new_with_mnemonic (_
						 ("О программе..."));
	gtk_widget_show (About);
	gtk_container_add (GTK_CONTAINER (menuitem4_menu), About);

	toolbar1 = gtk_toolbar_new ();
	gtk_widget_show (toolbar1);
	gtk_box_pack_start (GTK_BOX (vbox1), toolbar1, FALSE, FALSE, 0);
	gtk_toolbar_set_style (GTK_TOOLBAR (toolbar1), GTK_TOOLBAR_BOTH);
	tmp_toolbar_icon_size =
		gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar1));

	open_toolbutton =
		(GtkWidget *) gtk_tool_button_new_from_stock ("gtk-open");
	gtk_widget_show (open_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), open_toolbutton);

	separatortoolitem1 = (GtkWidget *) gtk_separator_tool_item_new ();
	gtk_widget_show (separatortoolitem1);
	gtk_container_add (GTK_CONTAINER (toolbar1), separatortoolitem1);

	copy_toolbutton =
		(GtkWidget *) gtk_tool_button_new_from_stock ("gtk-copy");
	gtk_widget_show (copy_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), copy_toolbutton);

	clear_toolbutton =
		(GtkWidget *) gtk_tool_button_new_from_stock ("gtk-clear");
	gtk_widget_show (clear_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), clear_toolbutton);

	font_toolbutton =
		(GtkWidget *)
		gtk_tool_button_new_from_stock ("gtk-select-font");
	gtk_widget_show (font_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), font_toolbutton);

	separatortoolitem2 = (GtkWidget *) gtk_separator_tool_item_new ();
	gtk_widget_show (separatortoolitem2);
	gtk_container_add (GTK_CONTAINER (toolbar1), separatortoolitem2);

	quit_toolbutton =
		(GtkWidget *) gtk_tool_button_new_from_stock ("gtk-quit");
	gtk_widget_show (quit_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), quit_toolbutton);

	about_toolbutton =
		(GtkWidget *) gtk_tool_button_new_from_stock ("gtk-about");
	gtk_widget_show (about_toolbutton);
	gtk_container_add (GTK_CONTAINER (toolbar1), about_toolbutton);

	scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow1);
	gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW
					     (scrolledwindow1),
					     GTK_SHADOW_IN);

	main_textview = gtk_text_view_new ();
	gtk_widget_show (main_textview);
	gtk_container_add (GTK_CONTAINER (scrolledwindow1), main_textview);
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (main_textview));
	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (main_textview),
				     GTK_WRAP_WORD);

	g_signal_connect ((gpointer) open, "activate",
			  G_CALLBACK (on_open_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) quit, "activate",
			  G_CALLBACK (on_quit_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) copy, "activate",
			  G_CALLBACK (on_copy_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) clear, "activate",
			  G_CALLBACK (on_clear_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) font, "activate",
			  G_CALLBACK (on_font_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) About, "activate",
			  G_CALLBACK (on_about_activate),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) open_toolbutton, "clicked",
			  G_CALLBACK (on_open_toolbutton_clicked),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) copy_toolbutton, "clicked",
			  G_CALLBACK (on_copy_toolbutton_clicked),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) clear_toolbutton, "clicked",
			  G_CALLBACK (on_clear_toolbutton_clicked),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) font_toolbutton, "clicked",
			  G_CALLBACK (on_font_toolbutton_clicked),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) quit_toolbutton, "clicked",
			  G_CALLBACK (on_quit_toolbutton_clicked),
			  (gpointer) main_textview);
	g_signal_connect ((gpointer) about_toolbutton, "clicked",
			  G_CALLBACK (on_about_toolbutton_clicked),
			  (gpointer) main_textview);

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_main_window,
				    easyreader_main_window,
				    "easyreader_main_window");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, vbox1, "vbox1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menubar1, "menubar1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem1, "menuitem1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem1_menu,
			     "menuitem1_menu");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, open, "open");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, separatormenuitem1,
			     "separatormenuitem1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, quit, "quit");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem2, "menuitem2");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem2_menu,
			     "menuitem2_menu");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, copy, "copy");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, clear, "clear");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem4, "menuitem4");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, menuitem4_menu,
			     "menuitem4_menu");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, About, "About");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, toolbar1, "toolbar1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, open_toolbutton,
			     "open_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, separatortoolitem1,
			     "separatortoolitem1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, copy_toolbutton,
			     "copy_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, clear_toolbutton,
			     "clear_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, font_toolbutton,
			     "font_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, separatortoolitem2,
			     "separatortoolitem2");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, quit_toolbutton,
			     "quit_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, about_toolbutton,
			     "about_toolbutton");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, scrolledwindow1,
			     "scrolledwindow1");
	GLADE_HOOKUP_OBJECT (easyreader_main_window, main_textview,
			     "main_textview");

	gtk_window_add_accel_group (GTK_WINDOW (easyreader_main_window),
				    accel_group);

	return easyreader_main_window;
}

GtkWidget *
create_easyreader_fileopen (void)
{
	GtkWidget *easyread_fileopen;
	GtkWidget *dialog_vbox1;
	GtkWidget *dialog_action_area1;
	GtkWidget *Cancel_button;
	GtkWidget *Open_button;
	GtkFileFilter *filter = gtk_file_filter_new ();

	gtk_file_filter_add_pattern (filter, "*.txt");
	gtk_file_filter_add_pattern (filter, "*.TXT");
	gtk_file_filter_add_pattern (filter, "*.rtf");
	gtk_file_filter_add_pattern (filter, "*.RTF");
	gtk_file_filter_add_pattern (filter, "*.fb2");
	gtk_file_filter_add_pattern (filter, "*.FB2");
	gtk_file_filter_set_name (filter,
				  "Файлы электронных книг");
	easyread_fileopen =
		gtk_file_chooser_dialog_new ("Выберите файл", NULL,
					     GTK_FILE_CHOOSER_ACTION_OPEN,
					     NULL);
	gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER
					      (easyread_fileopen), FALSE);
	if (path_to_file != NULL)
		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER
						     (easyread_fileopen),
						     path_to_file);
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (easyread_fileopen),
				     filter);
	gtk_window_set_type_hint (GTK_WINDOW (easyread_fileopen),
				  GDK_WINDOW_TYPE_HINT_DIALOG);

	combo_box = gtk_combo_box_new_text ();
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (UTF-8)");
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (CP866)");
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (CP1251)");
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (IBM855)");
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (ISO-8859-5)");
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box),
				   "Кириллица (KOI8-R)");
	gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
	gtk_widget_show (combo_box);
	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER
					   (easyread_fileopen), combo_box);

	dialog_vbox1 = GTK_DIALOG (easyread_fileopen)->vbox;
	gtk_widget_show (dialog_vbox1);

	dialog_action_area1 = GTK_DIALOG (easyread_fileopen)->action_area;
	gtk_widget_show (dialog_action_area1);
	gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1),
				   GTK_BUTTONBOX_END);

	Cancel_button = gtk_button_new_from_stock ("gtk-cancel");
	gtk_widget_show (Cancel_button);
	gtk_dialog_add_action_widget (GTK_DIALOG (easyread_fileopen),
				      Cancel_button, GTK_RESPONSE_CANCEL);
	GTK_WIDGET_SET_FLAGS (Cancel_button, GTK_CAN_DEFAULT);

	Open_button = gtk_button_new_from_stock ("gtk-open");
	gtk_widget_show (Open_button);
	gtk_dialog_add_action_widget (GTK_DIALOG (easyread_fileopen),
				      Open_button, GTK_RESPONSE_OK);
	GTK_WIDGET_SET_FLAGS (Open_button, GTK_CAN_DEFAULT);

	g_signal_connect ((gpointer) Open_button, "clicked",
			  G_CALLBACK (on_Open_button_clicked),
			  G_OBJECT (easyread_fileopen));
	g_signal_connect_swapped ((gpointer) Cancel_button, "clicked",
				  G_CALLBACK (gtk_widget_destroy),
				  G_OBJECT (easyread_fileopen));
	g_signal_connect_swapped ((gpointer) Open_button, "clicked",
				  G_CALLBACK (gtk_widget_destroy),
				  G_OBJECT (easyread_fileopen));

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (easyread_fileopen, easyread_fileopen,
				    "easyread_fileopen");
	GLADE_HOOKUP_OBJECT_NO_REF (easyread_fileopen, dialog_vbox1,
				    "dialog_vbox1");
	GLADE_HOOKUP_OBJECT_NO_REF (easyread_fileopen, dialog_action_area1,
				    "dialog_action_area1");
	GLADE_HOOKUP_OBJECT (easyread_fileopen, Cancel_button,
			     "Cancel_button");
	GLADE_HOOKUP_OBJECT (easyread_fileopen, Open_button, "Open_button");

	gtk_widget_grab_default (Open_button);
	return easyread_fileopen;
}

GtkWidget *
create_easyreader_about (void)
{
	GtkWidget *easyreader_about;
	const gchar *authors[] = {
		"Александр Теленьга <telenga@gmail.com> - основной разработчик",
		"Tarot Osuji <tarot@sdf.lonestar.org> - функции изменения шрифта",
		"Constantin Radchenko <conr@users.sourceforge.net> - парсер fb2",
		"Brandon Long <blong@fiction.net> - парсер rtf",
		NULL
	};
	/* TRANSLATORS: Replace this string with your names, one name per line. */
	gchar *translators = _("translator-credits");

	easyreader_about = gtk_about_dialog_new ();
	gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (easyreader_about),
				      VERSION);
	gtk_about_dialog_set_name (GTK_ABOUT_DIALOG (easyreader_about),
				   _("Easy reader"));
	gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (easyreader_about),
					_
					("Александр Теленьга"));
	gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (easyreader_about),
				       _
				       ("Программа для чтения электронных книг"));
	gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (easyreader_about),
				      authors);
	gtk_about_dialog_set_translator_credits (GTK_ABOUT_DIALOG
						 (easyreader_about),
						 translators);

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_about, easyreader_about,
				    "easyreader_about");

	return easyreader_about;
}

GtkWidget *
create_easyreader_fontselectiondialog (void)
{
	GtkWidget *easyreader_fontselectiondialog;
	GtkWidget *ok_button;
	GtkWidget *cancel_button;
	GtkWidget *font_selection;
	gchar *current_fontname;

	easyreader_fontselectiondialog =
		gtk_font_selection_dialog_new (_
					       ("Выберите шрифт"));
	gtk_container_set_border_width (GTK_CONTAINER
					(easyreader_fontselectiondialog), 4);
	gtk_window_set_type_hint (GTK_WINDOW (easyreader_fontselectiondialog),
				  GDK_WINDOW_TYPE_HINT_DIALOG);
	gtk_window_set_transient_for (GTK_WINDOW
				      (easyreader_fontselectiondialog),
				      GTK_WINDOW (easyreader_main_window));
	current_fontname = get_font_name_from_widget (main_textview);
	gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG
						 (easyreader_fontselectiondialog),
						 current_fontname);

	ok_button =
		GTK_FONT_SELECTION_DIALOG (easyreader_fontselectiondialog)->
		ok_button;
	gtk_widget_show (ok_button);
	GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);

	cancel_button =
		GTK_FONT_SELECTION_DIALOG (easyreader_fontselectiondialog)->
		cancel_button;
	gtk_widget_show (cancel_button);
	GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);

	font_selection =
		GTK_FONT_SELECTION_DIALOG (easyreader_fontselectiondialog)->
		fontsel;
	gtk_widget_show (font_selection);
	gtk_container_set_border_width (GTK_CONTAINER (font_selection), 4);
	gtk_font_selection_set_preview_text (GTK_FONT_SELECTION
					     (font_selection),
					     _
					     ("\320\260\320\261\320\262\320\263\320\264\320\265 \320\220\320\221\320\222\320\223\320\224\320\225 abcde ABCDE"));

	g_signal_connect ((gpointer) ok_button, "clicked",
			  G_CALLBACK (on_ok_button_clicked),
			  G_OBJECT (easyreader_fontselectiondialog));
	g_signal_connect_swapped ((gpointer) cancel_button, "clicked",
				  G_CALLBACK (gtk_widget_destroy),
				  G_OBJECT (easyreader_fontselectiondialog));
	g_signal_connect_swapped ((gpointer) ok_button, "clicked",
				  G_CALLBACK (gtk_widget_destroy),
				  G_OBJECT (easyreader_fontselectiondialog));

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_fontselectiondialog,
				    easyreader_fontselectiondialog,
				    "easyreader_fontselectiondialog");
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_fontselectiondialog, ok_button,
				    "ok_button");
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_fontselectiondialog,
				    cancel_button, "cancel_button");
	GLADE_HOOKUP_OBJECT_NO_REF (easyreader_fontselectiondialog,
				    font_selection, "font_selection");

	return easyreader_fontselectiondialog;
}
